//
//  AGIPCAssetsController.m
//  AGImagePickerController
//
//  Created by Artur Grigor on 17.02.2012.
//  Copyright (c) 2012 - 2013 Artur Grigor. All rights reserved.
//  
//  For the full copyright and license information, please view the LICENSE
//  file that was distributed with this source code.
//  

#import "AGIPCAssetsController.h"

#import "AGImagePickerController+Helper.h"
#import "AGIPCGridCell.h"
#import "AGIPCToolbarItem.h"
#import "AGIPCPreviewController.h"


@interface AGIPCAssetsController ()<AGIPCPreviewControllerDelegate>
{
    ALAssetsGroup *_assetsGroup;
    NSMutableArray *_assets;
    NSMutableArray *_selectedAssets;
    
    __ag_weak AGImagePickerController *_imagePickerController;
    
    UIInterfaceOrientation lastOrientation;
}
@property (nonatomic, strong) UILabel *selectStateLbl;
@property (nonatomic, strong) UIBarButtonItem *previewItem;
@property (nonatomic, strong) NSMutableArray *assets;
@end

@interface AGIPCAssetsController (Private)
- (void)changeSelectionInformation;
- (void)didChangeLibrary:(NSNotification *)notification;
- (void)didChangeToolbarItemsForManagingTheSelection:(NSNotification *)notification;

- (BOOL)toolbarHidden;
- (void)loadAssets;
- (void)reloadData;

- (void)setupToolbarItems;
- (NSArray *)itemsForRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)doneAction:(id)sender;
- (void)customBarButtonItemAction:(id)sender;

- (void)registerForNotifications;
- (void)unregisterFromNotifications;
@end


@implementation AGIPCAssetsController
@synthesize assetsGroup = _assetsGroup, assets = _assets, imagePickerController = _imagePickerController;

- (BOOL)toolbarHidden {
    if (! self.imagePickerController.shouldShowToolbarForManagingTheSelection)
        return YES;
    else {
        if (self.imagePickerController.toolbarItemsForManagingTheSelection != nil) {
            return !(self.imagePickerController.toolbarItemsForManagingTheSelection.count > 0);
        } else {
            return NO;
        }
    }
}

- (void)setAssetsGroup:(ALAssetsGroup *)theAssetsGroup {
    @synchronized (self) {
        if (_assetsGroup != theAssetsGroup) {
            _assetsGroup = theAssetsGroup;
            [_assetsGroup setAssetsFilter:[ALAssetsFilter allPhotos]];
            // modified by springox(20140510)
            //[self reloadData];
        }
    }
}

- (ALAssetsGroup *)assetsGroup {
    ALAssetsGroup *ret = nil;
    @synchronized (self) {
        ret = _assetsGroup;
    }
    return ret;
}

- (NSArray *)selectedAssets {
    NSMutableArray *selectedAssets = [NSMutableArray arrayWithArray:_selectedAssets];
	for (AGIPCGridItem *gridItem in self.assets)  {
		if (gridItem.selected && ![selectedAssets containsObject:gridItem.asset]) {
			[selectedAssets addObject:gridItem.asset];
		}
	}
    return selectedAssets;
}

#pragma mark - Object Lifecycle
- (id)initWithImagePickerController:(AGImagePickerController *)imagePickerController andAssetsGroup:(ALAssetsGroup *)assetsGroup {
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        self.assetsGroup = assetsGroup;
        _assets = [[NSMutableArray alloc] init];
        _selectedAssets = [[NSMutableArray alloc] init];
        self.imagePickerController = imagePickerController;
//        self.title = [self.assetsGroup valueForProperty:ALAssetsGroupPropertyName];
        self.title = @"所有照片";
        [self setupToolbarItems]; // Setup toolbar items
        [self loadAssets];// Start loading the assets
    }
    return self;
}


#pragma mark - UITableViewDataSource Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (! self.imagePickerController)
        return 0;
    double numberOfAssets = (double)self.assetsGroup.numberOfAssets;
    NSInteger nr = ceil(numberOfAssets / self.imagePickerController.numberOfItemsPerRow);
    return nr;
}

- (NSArray *)itemsForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *items = [NSMutableArray arrayWithCapacity:self.imagePickerController.numberOfItemsPerRow];
    NSUInteger startIndex = indexPath.row * self.imagePickerController.numberOfItemsPerRow;
    NSUInteger endIndex = startIndex + self.imagePickerController.numberOfItemsPerRow - 1;
    if (startIndex < self.assets.count) {
        if (endIndex > self.assets.count - 1)
            endIndex = self.assets.count - 1;
        for (NSUInteger i = startIndex; i <= endIndex; i++) {
            [items addObject:(self.assets)[i]];
        }
    }
    return items;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.imagePickerController.itemRect.origin.y;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGRect itemRect = self.imagePickerController.itemRect;
    return itemRect.size.height + itemRect.origin.y;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    AGIPCGridCell *cell = (AGIPCGridCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)  {
        cell = [[AGIPCGridCell alloc] initWithImagePickerController:self.imagePickerController items:[self itemsForRowAtIndexPath:indexPath] andReuseIdentifier:CellIdentifier];
    } else  {
		cell.items = [self itemsForRowAtIndexPath:indexPath];
	}
    return cell;
}

#pragma mark - View Lifecycle
- (void)doneAction:(id)sender {
    [self.imagePickerController performSelector:@selector(didFinishPickingAssets:) withObject:self.selectedAssets];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.allowsMultipleSelection = NO;
    self.tableView.allowsSelection = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // Navigation Bar Items
    UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)]; //完成操作
    doneButtonItem.enabled = NO;
    self.navigationItem.rightBarButtonItem = doneButtonItem;
    lastOrientation = self.interfaceOrientation;
    
    // modified by springox(20140510)
    [self reloadData];
    [self registerForNotifications]; // Setup Notifications
    [AGIPCGridItem performSelector:@selector(resetNumberOfSelections)];// add by springox(20141105)
}

- (UILabel *)selectStateLbl {
    if (!_selectStateLbl) {
        _selectStateLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50.0f, 30.0f)];
        _selectStateLbl.font = [UIFont systemFontOfSize:17];
        _selectStateLbl.backgroundColor = [UIColor clearColor];
        _selectStateLbl.textColor = [UIColor blueColor];
        _selectStateLbl.textAlignment = NSTextAlignmentLeft;
    }
    return _selectStateLbl;
}

#pragma mark - Private
- (void)setupToolbarItems {
    if (self.imagePickerController.toolbarItemsForManagingTheSelection != nil) {
        NSMutableArray *items = [NSMutableArray array];
        for (id item in self.imagePickerController.toolbarItemsForManagingTheSelection) { // Custom Toolbar Items
            NSAssert([item isKindOfClass:[AGIPCToolbarItem class]], @"Item is not a instance of AGIPCToolbarItem.");
            ((AGIPCToolbarItem *)item).barButtonItem.target = self;
            ((AGIPCToolbarItem *)item).barButtonItem.action = @selector(customBarButtonItemAction:);
            [items addObject:((AGIPCToolbarItem *)item).barButtonItem];
        }
        self.toolbarItems = items;
    } else {
        UIBarButtonItem *myLabelItem = [[UIBarButtonItem alloc] initWithCustomView:self.selectStateLbl];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        self.previewItem = [[UIBarButtonItem alloc] initWithTitle:@"预览" style:UIBarButtonItemStyleBordered target:self action:@selector(previewAction:)];
        NSArray *toolbarItemsForManagingTheSelection = @[myLabelItem, flexibleSpace, self.previewItem];
        self.toolbarItems = toolbarItemsForManagingTheSelection;
    }
}
- (void)customBarButtonItemAction:(id)sender {
    for (id item in self.imagePickerController.toolbarItemsForManagingTheSelection) {
        NSAssert([item isKindOfClass:[AGIPCToolbarItem class]], @"Item is not a instance of AGIPCToolbarItem.");
        if (((AGIPCToolbarItem *)item).barButtonItem == sender) {
            if (((AGIPCToolbarItem *)item).assetIsSelectedBlock) {
                NSUInteger idx = 0;
                for (AGIPCGridItem *obj in self.assets) {
                    obj.selected = ((AGIPCToolbarItem *)item).assetIsSelectedBlock(idx, ((AGIPCGridItem *)obj).asset);
                    idx++;
                }
            }
        }
    }
}
- (void)previewAction:(id)sender {
    lastOrientation = self.interfaceOrientation;
    NSMutableArray *selectedAssets = [NSMutableArray array];
    for (AGIPCGridItem *gridItem in self.assets)  {
        if (gridItem.selected && ![selectedAssets containsObject:gridItem.asset]) {
            [selectedAssets addObject:gridItem];
        }
    }
    AGIPCPreviewController *preController = [[AGIPCPreviewController alloc] initWithAssets:selectedAssets targetAsset:selectedAssets[0]];
    preController.delegate = self;
    preController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController presentViewController:preController animated:YES completion:nil];
}


//加载所有图片
- (void)loadAssets {
    // add by springox(20150720)
    [AGIPCGridItem performSelector:@selector(resetNumberOfSelections)];
    [self.assets removeAllObjects];
    
    __ag_weak AGIPCAssetsController *weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        __strong AGIPCAssetsController *strongSelf = weakSelf;
        @autoreleasepool {
            [strongSelf.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if (result == nil)  {
                    return;
                }
                if (strongSelf.imagePickerController.shouldShowPhotosWithLocationOnly) {
                    CLLocation *assetLocation = [result valueForProperty:ALAssetPropertyLocation];
                    if (!assetLocation || !CLLocationCoordinate2DIsValid([assetLocation coordinate])) {
                        return;
                    }
                }
                AGIPCGridItem *gridItem = [[AGIPCGridItem alloc] initWithImagePickerController:self.imagePickerController asset:result andDelegate:self];
                // Descending photos, springox(20131225)
                [strongSelf.assets addObject:gridItem];
                //[strongSelf.assets insertObject:gridItem atIndex:0];
            }];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [strongSelf reloadData];
        });
    });
}

- (void)reloadData {
    // Don't display the select button until all the assets are loaded.
    [self.navigationController setToolbarHidden:[self toolbarHidden] animated:YES];
    [self.tableView reloadData];
    
    //[self setTitle:[self.assetsGroup valueForProperty:ALAssetsGroupPropertyName]];
    [self changeSelectionInformation];
    NSInteger totalRows = [self.tableView numberOfRowsInSection:0]; //Prevents crash if totalRows = 0 (when the album is empty).
    if (totalRows > 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:totalRows-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
}

#warning 此次需要改动
//顶部选择的信息
- (void)changeSelectionInformation {
    self.navigationItem.rightBarButtonItem.enabled = ([AGIPCGridItem numberOfSelections] > 0);
    if (self.imagePickerController.shouldDisplaySelectionInformation) {
        NSInteger maxNumber = _imagePickerController.maximumNumberOfPhotosToBeSelected;
        self.selectStateLbl.text = [NSString stringWithFormat:@"(%lu/%ld)", (unsigned long)[AGIPCGridItem numberOfSelections], (long)maxNumber];
        
        if ([AGIPCGridItem numberOfSelections] == 0) {
            self.previewItem.enabled = NO;
        } else {
            self.previewItem.enabled = YES;
        }
    }
}


#pragma mark -  AGIPCGridItemDelegate
- (void)agGridItem:(AGIPCGridItem *)gridItem didChangeNumberOfSelections:(NSNumber *)numberOfSelections {
    [self changeSelectionInformation];
}
//判断选择状态、或是否已达到最大选取个数
- (BOOL)agGridItemCanSelect:(AGIPCGridItem *)gridItem {
    if (self.imagePickerController.selectionMode == AGImagePickerControllerSelectionModeSingle && self.imagePickerController.selectionBehaviorInSingleSelectionMode == AGImagePickerControllerSelectionBehaviorTypeRadio) {
        for (AGIPCGridItem *item in self.assets)
            if (item.selected)
                item.selected = NO;
        return YES;
    } else {
        if (self.imagePickerController.maximumNumberOfPhotosToBeSelected > 0)
            return ([AGIPCGridItem numberOfSelections] < self.imagePickerController.maximumNumberOfPhotosToBeSelected);
        else
            return YES;
    }
}

// add by springox(20141023)  点击图片，进入预览页面
- (void)agGridItemDidTapAction:(AGIPCGridItem *)gridItem {
    // mark the original orientation, springox(20141109)
    lastOrientation = self.interfaceOrientation;
    AGIPCPreviewController *preController = [[AGIPCPreviewController alloc] initWithAssets:self.assets targetAsset:gridItem];
    preController.delegate = self;
    preController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController presentViewController:preController animated:YES completion:nil];
}

// add by springox(20150712)  图片选取或取消
- (void)agGridItemDidTapCheckMarkAction:(AGIPCGridItem *)gridItem {
    if (nil == _selectedAssets) {
        _selectedAssets = [[NSMutableArray alloc] init];
    }
    if (gridItem.selected) {
        if ([_selectedAssets containsObject:gridItem.asset]) {
            [_selectedAssets removeObject:gridItem.asset];
        }
        [_selectedAssets addObject:gridItem.asset];
    } else {
        [_selectedAssets removeObject:gridItem.asset];
    }
}

#pragma mark - 旋转
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (lastOrientation != self.interfaceOrientation) {
        [self reloadData];
    }
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
- (BOOL)shouldAutorotate {
    return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self reloadData];
}
// add by springox(20141024)
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    [self reloadData];
}


#pragma mark - AGIPCPreviewControllerDelegate Methods
- (void)previewController:(AGIPCPreviewController *)pVC didRotateFromOrientation:(UIInterfaceOrientation)fromOrientation {
    // do noting
}


#pragma mark - Notifications
- (void)dealloc {
    [self unregisterFromNotifications];
}
- (void)registerForNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeLibrary:) name:ALAssetsLibraryChangedNotification object:[AGImagePickerController defaultAssetsLibrary]];
}
- (void)unregisterFromNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self  name:ALAssetsLibraryChangedNotification object:[AGImagePickerController defaultAssetsLibrary]];
}
- (void)didChangeLibrary:(NSNotification *)notification {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loadAssets];
    });
}

- (void)didChangeToolbarItemsForManagingTheSelection:(NSNotification *)notification {
    NSLog(@"here.%@", notification);
}

@end
